package com.keyancompany.zhongbaopingtai.activity;

import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.api.UserAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.User;
import com.keyancompany.zhongbaopingtai.utils.KeyBoardUtils;
import com.keyancompany.zhongbaopingtai.utils.T;

import butterknife.Bind;
import butterknife.BindString;
import butterknife.OnClick;

/**
 * Created by firefox on 2017/2/12.
 */

public class PayPasswordActivity extends BaseActivity {
    @BindString(R.string.setting_pay_password_text)
    String title;
    @Bind(R.id.pay_login_password)
    EditText payLoginPassword;
    @Bind(R.id.pay_password)
    EditText payPassword;
    @Bind(R.id.pay_confrim_password)
    EditText payConfrimPassword;
    @Bind(R.id.submit)
    TextView submit;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    private boolean isFirst = false;
    private User user;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_pay_password;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        isFirst = UserAPI.getCurrentUser().getCashpwd() == 0;
        if (!isFirst)
            title = getString(R.string.update_pay_password);
        initToolBar(toolbar, title);
    }

    @Override
    protected void setUpViews() {
        if (isFirst) {
            payLoginPassword.setVisibility(View.GONE);
        }
    }

    @OnClick(R.id.submit)
    public void submitFirm(View v) {
        String loginStr = payLoginPassword.getText().toString();
        if (!isFirst && TextUtils.isEmpty(loginStr)) {
            T.show(PayPasswordActivity.this, "旧密码不能为空", Toast.LENGTH_SHORT);
            return;
        }
        if (!isFirst && lengthError(loginStr)) {
            return;
        }
        String payStr = payPassword.getText().toString();
        if (TextUtils.isEmpty(payStr)) {
            T.show(PayPasswordActivity.this, "新支付密码不能为空", Toast.LENGTH_SHORT);
            return;
        }
        if (lengthError(payStr)) {
            return;
        }
        String payAgain = payConfrimPassword.getText().toString();
        if (TextUtils.isEmpty(payAgain)) {
            T.show(PayPasswordActivity.this, "确认密码不能为空", Toast.LENGTH_SHORT);
            return;
        }
        if (lengthError(payAgain)) {
            return;
        }
        if (!payStr.equals(payAgain)) {
            T.show(PayPasswordActivity.this, "两次支付密码不相同", Toast.LENGTH_SHORT);
            return;
        }


        UserAPI.editCashPw(isFirst ? loginStr : null, payStr, this, getLoading(-1));
    }

    boolean lengthError(String value) {
        if (value.length() < 6) {
            T.show(PayPasswordActivity.this, "密码长度小于6", Toast.LENGTH_SHORT);
            return true;
        }
        return false;
    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);
        BaseModel base = validateJson(json);
        if (base.isSuccess()) {
            user.setCashpwd(1);
            finish();
        }
    }

    @Override
    void closeBroad() {
        KeyBoardUtils.closeKeybord(payLoginPassword, this);
    }
}
